package com.member.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.api.TestClientDataAndSignGuoJiDemo;
import com.member.dto.ContractDatails;
import com.member.dto.ContractManagement;
import com.member.dto.SignQuery;
import com.member.dto.SignResultInform;
import com.member.model.DES3Utils;
import com.member.provider.mapper.MemberSignMapper;
import com.member.provider.service.MemberSignService;
import com.member.utils.Result;
import com.member.vo.SignMessageReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther MemberSignServiceImpl
 * @date 2023/12/8 10:11
 */
@Service
public class MemberSignServiceImpl extends ServiceImpl<MemberSignMapper,ContractManagement> implements MemberSignService {

    @Autowired
    private MemberSignMapper memberSignMapper;

    /**
     * @Description TODO
     * @param signResultInform
     * @retuen result
     * @date 2024/1/3 15:09
     * @auther 银狐先生
     * 签约结果通知
     */
    @Override
    public Result signResultInform(SignResultInform signResultInform) {

        String userName=signResultInform.getUserName();
        String cardID=signResultInform.getCardId();
        int signStatus=signResultInform.getSignStatus();
        Date timestamp=signResultInform.getTimestamp();
        String yhfToken=signResultInform.getYhfToken();
        int cardType=signResultInform.getCardType();
        String issInsCode=signResultInform.getIssInsCode();
        String bankCardNo=signResultInform.getBankCardNo();
        Object objectData=signResultInform.getObjectData();
        String channelId=signResultInform.getChannelId();
        try {
            userName = TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getUserName(),"11111111");
            cardID=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getCardId(),"22222222");
            timestamp=new Date();
            yhfToken=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getYhfToken(),"33333333");
            if (cardType==2){
                issInsCode="";
                bankCardNo="BANK000201";
                bankCardNo=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getBankCardNo(),"44444444");
                channelId="YHFQXXXXX&ban kProductId=BANK00201&bankProductName=交行惠民就医";
                channelId=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getChannelId(),"55555555");
            }else{
                bankCardNo=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getBankCardNo(),"44444444");
                //返回银行简码

                channelId="YHFQXXXXX";
                channelId=TestClientDataAndSignGuoJiDemo.encryptDESCBC(signResultInform.getChannelId(),"55555555");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (signStatus==1){
            return  Result.success("签约成功");
        }else{
            return  Result.error("解约");
        }

    }


    /**
     * @Description TODO
     * @param signResult
     * @retuen SignResult
     * @date 2023/12/18 17:05
     * @auther 银狐先生
     */
    @Override
    public SignMessageReqVo signSelectResult(SignMessageReqVo signResult) {
        return null;
    }

    /**
     * @Description TODO
     * @param  
     * @retuen 
     * @date 2024/2/21 11:10
     * @auther 银狐先生 
     */
    @Override
    public Result getSignsV2(SignQuery signQueryq) {
//        TestYFHInterfaceByChannelIdSignV2.
        return null;
    }

    /**
     * @Description TODO
     * @param ContractResult
     * @retuen
     * @date 2024/2/22 9:24
     * @auther 银狐先生
     */
//    @Override
//    public Result updateContractStatus(ContractResult contractResult) {
//        // 根据银联推送的结果更新用户签约状态
//        // 可能需要根据contractResult中的信息查询用户信息，并更新数据库中的签约状态
//        return null;
//    }


    @Override
    public Result selectSignMessage(SignMessageReqVo signMessageVo)  {

        Map<String, String> json = new HashMap<>();
                String userName=signMessageVo.getUserName();
                String cardID=signMessageVo.getCartNo();
                json.put("channelId", "xxxxxxxxxx"); // 接入方id，固定值，银联提供
                json.put("hospitalId", "xxxxxxxxxx"); // hospitalId 银联提供
                int cardType = signMessageVo.getCardType();
        try {
            String encryptedUserName = DES3Utils.encryptDESCBC(userName,"1231980980989811");
            String encryptedCardId = DES3Utils.encryptDESCBC(cardID,"126725635276537625367111");

        } catch (Exception e) {
            e.printStackTrace();
        }

        json.put("userName", userName);

        json.put("cardID", cardID);
        json.put("cardType", "0");
        json.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

        ContractDatails contractDatails =memberSignMapper.selectSignMessage(json);
        return Result.success(contractDatails);
    }

    @Override
    public Result selectSignMessageBySignId(String channelId) {
        List<ContractManagement> contractManagementList= memberSignMapper.selectSignMessageBySignId(channelId);
        return Result.success(contractManagementList);
    }




}
